use std::fs::File;
use std::io::{self, Write};
use std::path::Path;

// 引入模块
use library::config_loader;
use library::language_finder;
use library::project_writer;

fn main() -> Result<(), io::Error> {
    let args: Vec<String> = std::env::args().collect();
    let common_config = config_loader::load_common_config()?;
    let language = if args.len() != 2 {
        if let Some(ref default_lang) = common_config.default_lang {
            default_lang.clone()
        } else {
            let available_languages = language_finder::get_available_languages()?;
            println!("Available languages in the 'lang' directory:");
            for lang in &available_languages {
                println!("  - {}", lang);
            }
            print!("Please enter the language name: ");
            io::stdout().flush()?;
            let mut input = String::new();
            io::stdin().read_line(&mut input)?;
            input.trim().to_string()
        }
    } else {
        args[1].clone()
    };

    let config = config_loader::load_config(&language)?;
    let root = Path::new(&config.root_directory);
    let mut output_file = File::create(&common_config.output_file)?;

    project_writer::write_project_structure(root, &config, &mut output_file)?;

    println!("Project structure and files content written to {}", common_config.output_file);
    Ok(())
}